<?php


namespace app\api\unionpay\ysf;

class RequestHandler
{
	public $gateUrl;
	public $key;
	public $private_rsa_key;
	public $signtype;
	public $parameters;
	public $debugInfo;
	public function __construct()
	{
		$this->RequestHandler();
	}
	public function RequestHandler()
	{
		$this->gateUrl = "";
		$this->key = "";
		$this->private_rsa_key = "";
		$this->signtype = "";
		$this->parameters = [];
		$this->debugInfo = "";
	}
	public function init()
	{
	}
	public function getGateURL()
	{
		return $this->gateUrl;
	}
	public function setGateURL($gateUrl)
	{
		$this->gateUrl = $gateUrl;
	}
	public function setSignType($type)
	{
		$this->signtype = $type;
	}
	public function getKey()
	{
		return $this->key;
	}
	public function setKey($key)
	{
		$this->key = $key;
	}
	public function setRSAKey($key)
	{
		$this->private_rsa_key = $key;
	}
	public function getParameter($parameter)
	{
		return isset($this->parameters[$parameter]) ? $this->parameters[$parameter] : "";
	}
	public function setParameter($parameter, $parameterValue)
	{
		$this->parameters[$parameter] = $parameterValue;
	}
	public function setReqParams($post, $filterField = null)
	{
		if ($filterField !== null) {
			foreach ($filterField as $k => $v) {
				unset($post[$v]);
			}
		}
		foreach ($post as $k => $v) {
			if (empty($v)) {
				unset($post[$k]);
			}
		}
		$this->parameters = $post;
	}
	public function getAllParameters()
	{
		return $this->parameters;
	}
	public function getRequestURL()
	{
		$this->createSign();
		$reqPar = "";
		ksort($this->parameters);
		foreach ($this->parameters as $k => $v) {
			$reqPar .= $k . "=" . urlencode($v) . "&";
		}
		$reqPar = substr($reqPar, 0, strlen($reqPar) - 1);
		$requestURL = $this->getGateURL() . "?" . $reqPar;
		return $requestURL;
	}
	public function getDebugInfo()
	{
		return $this->debugInfo;
	}
	public function createSign()
	{
		if ($this->signtype == "MD5") {
			$this->createMD5Sign();
		} else {
			$this->createRSASign();
		}
	}
	public function createMD5Sign()
	{
		$signPars = "";
		ksort($this->parameters);
		foreach ($this->parameters as $k => $v) {
			if ("" != $v && "sign" != $k) {
				$signPars .= $k . "=" . $v . "&";
			}
		}
		$signPars .= "key=" . $this->getKey();
		$sign = strtoupper(md5($signPars));
		$this->setParameter("sign", $sign);
		$this->_setDebugInfo($signPars . " => sign:" . $sign);
	}
	public function createRSASign()
	{
		$signPars = "";
		ksort($this->parameters);
		foreach ($this->parameters as $k => $v) {
			if ("" != $v && "sign" != $k) {
				$signPars .= $k . "=" . $v . "&";
			}
		}
		$signPars = substr($signPars, 0, strlen($signPars) - 1);
		$res = openssl_get_privatekey($this->private_rsa_key);
		if ($this->signtype == "RSA_1_1") {
			openssl_sign($signPars, $sign, $res);
		} elseif ($this->signtype == "RSA_1_256") {
			openssl_sign($signPars, $sign, $res, OPENSSL_ALGO_SHA256);
		}
		openssl_free_key($res);
		$sign = base64_encode($sign);
		$this->setParameter("sign", $sign);
		$this->_setDebugInfo($signPars . " => sign:" . $sign);
	}
	public function _setDebugInfo($debugInfo)
	{
		$this->debugInfo = $debugInfo;
	}
}